local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);

local PlayerBattleInfoGui=ClientOHOH.load.UIManager().ui_list.PlayerBattleInfoGui

local Tool=require(script.Parent.tool) 


function M.new(self,...)
	return M(self,...);
end;



function M.ctor(self, parent)
	M.super.ctor(self, parent);	
    self.LastLevel=0
    self.player=self.parent

    self.DataBase=self.player:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
    self:Update()

    self.parent:AttributeValueChanged("Hp",function()

        local damge= self.hp-self.player.attribute.Hp
       if damge>0 then --受伤了
            _G.ClientOHOH.load.UIManager().ui_list.PlayerBattleInfoGui:AddItem("base_hp",-damge)
       end
         self:Update()
         --死亡了，退出战斗
         if self.hp==0 then
            self.player:GetComponent(_G.CommonOHOH.load.Common_AttackCtrl()):ExitBattle()
         end

    end)

    self.parent:AttributeValueChanged("MaxHp",function()
        self:Update()
    end)

    self.parent:AttributeValueChanged("Level",function()
        if self.player.attribute.Level>self.level then -- 播放升级效果
            self:PlayLevelUpEffect()
        end
        self:Update()
        self.DataBase:GetData("base").level=self.level
    end)

    self.parent:AttributeValueChanged("Exp",function()
        self:Update()
        self.DataBase:GetData("base").exp=self.exp
    end)

	PlayerBattleInfoGui:Show()
end

local LevelUpEffecrPerfab=ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.Effect.LevelUp")
local LanguageHelper=_G.LanguageHelper 
local Client_AudioManager = ClientOHOH.load.Client_AudioManager();

function M.PlayLevelUpEffect(self)
    local e=LevelUpEffecrPerfab:Clone()
    assert(self.player.character,"self.character==null")
    require(e.PlayCtrl).Play(self.player.character:GetPosition())
    ClientOHOH.load.UIManager().ui_list.GlobalTipGui:Show(LanguageHelper:GetText(30015))
    Client_AudioManager:PlayGloble("upgrade");

end

function M.Update(self)
    self.hp=self.player.attribute.Hp
    self.exp=self.player.attribute.Exp
    self.maxHp=self.player.attribute.MaxHp
    self.level=self.player.attribute.Level
    PlayerBattleInfoGui:UpdateLevel(self.level)
	PlayerBattleInfoGui:UpdateExp(self.exp,Tool.UpgradeExp(self.level,self.player))
	PlayerBattleInfoGui:UpdateHp(self.hp,self.maxHp)

    if self.LastLevel~= self.level then
        local oldLevel=self.LastLevel
        self.LastLevel=self.level
        self.DataBase:SimulateModifyData({"base","level"},self.level)
        self.player.LevelChangeEvent:Action(self.level,oldLevel)
    end
    self.DataBase:SimulateModifyData({"base","exp"},self.exp)
end

function M.dtor(self)
	M.super.dtor(self);
end;




return M